package org.slf4j.impl;

import org.slf4j.Logger;
import org.slf4j.Marker;

import tptecnicas.logging.LogTool;


/**
 * Clase Adaptador para habilitar y compatibilizar 
 * el uso de la presente libreria de Log en SLF4J.
 */
public class TpTecniLoggerAdapter implements Logger {

	private tptecnicas.logging.Logger logger;
	private final String TRACE_LEVEL_NAME = "TRACE";
	private final String DEBUG_LEVEL_NAME = "DEBUG";
	private final String INFO_LEVEL_NAME = "INFO";
	private final String WARNING_LEVEL_NAME = "WARNING";
	private final String ERROR_LEVEL_NAME = "ERROR";

	public TpTecniLoggerAdapter(String name) {
		logger = LogTool.getLogTool().getLogger(name);		
	}

	@Override
	public String getName() {
		return logger.getLoggerName();
	}

	@Override
	public boolean isTraceEnabled() {
		return this.logger.isLevelEnabled(TRACE_LEVEL_NAME);
	}

	@Override
	public void trace(String msg) {
		this.logger.logMessage(TRACE_LEVEL_NAME, msg);
	}

	@Override
	public void trace(String format, Object arg) {
		if(isTraceEnabled())
		{
			trace(format.replace("{0}", arg.toString()));
		}
	}

	@Override
	public void trace(String format, Object arg1, Object arg2) {
		if(isTraceEnabled())
		{
			trace(format.replace("{0}", arg1.toString()).replace("{1}", arg2.toString()));
		}
		
	}

	@Override
	public void trace(String format, Object... arguments) {
		if(isTraceEnabled())
		{
			String output = format;
			int i = 0;
			for(Object arg : arguments) {
				output = output.replaceFirst("{"+ i +"}", arg.toString());
				i++;
			}
			trace(output);
		}
	}

	@Override
	public void trace(String msg, Throwable t) {
		this.logger.logMessageWithException(TRACE_LEVEL_NAME, msg, t);
	}

	@Override
	public boolean isTraceEnabled(Marker marker) {
		//ignore marker..
		return this.isTraceEnabled();
	}

	@Override
	public void trace(Marker marker, String msg) {
		//ignore marker..
		this.trace(msg);		
	}

	@Override
	public void trace(Marker marker, String format, Object arg) {
		//ignore marker..
		this.trace(format, arg);
	}

	@Override
	public void trace(Marker marker, String format, Object arg1, Object arg2) {
		//ignore marker..
		this.trace(format, arg1, arg2);
	}

	@Override
	public void trace(Marker marker, String format, Object... argArray) {
		//ignore marker..
		this.trace(format, argArray);
	}

	@Override
	public void trace(Marker marker, String msg, Throwable t) {
		//ignore marker..
		this.trace(msg, t);
	}

	@Override
	public boolean isDebugEnabled() {
		return this.logger.isLevelEnabled(DEBUG_LEVEL_NAME);
	}

	@Override
	public void debug(String msg) {
		this.logger.logMessage(DEBUG_LEVEL_NAME, msg);
	}

	@Override
	public void debug(String format, Object arg) {
		if(isDebugEnabled())
		{
			debug(format.replace("{0}", arg.toString()));
		}
	}

	@Override
	public void debug(String format, Object arg1, Object arg2) {
		if(isDebugEnabled())
		{
			debug(format.replace("{0}", arg1.toString()).replace("{1}", arg2.toString()));
		}
	}

	@Override
	public void debug(String format, Object... arguments) {
		if(isDebugEnabled())
		{
			String output = format;
			int i = 0;
			for(Object arg : arguments) {
				output = output.replaceFirst("{"+ i +"}", arg.toString());
				i++;
			}
			debug(output);
		}
	}

	@Override
	public void debug(String msg, Throwable t) {
		this.logger.logMessageWithException(DEBUG_LEVEL_NAME, msg, t);
	}

	@Override
	public boolean isDebugEnabled(Marker marker) {
		//ignore marker..
		return this.isDebugEnabled();
	}

	@Override
	public void debug(Marker marker, String msg) {
		//ignore marker..
		this.debug(msg);
	}

	@Override
	public void debug(Marker marker, String format, Object arg) {
		//ignore marker..
		this.debug(format, arg);
	}

	@Override
	public void debug(Marker marker, String format, Object arg1, Object arg2) {
		//ignore marker..
		this.debug(format, arg1, arg2);
	}

	@Override
	public void debug(Marker marker, String format, Object... arguments) {
		//ignore marker..
		this.debug(format, arguments);
	}

	@Override
	public void debug(Marker marker, String msg, Throwable t) {
		//ignore marker..
		this.debug(msg, t);
	}

	@Override
	public boolean isInfoEnabled() {
		return this.logger.isLevelEnabled(INFO_LEVEL_NAME);
	}

	@Override
	public void info(String msg) {
		this.logger.logMessage(INFO_LEVEL_NAME, msg);
	}

	@Override
	public void info(String format, Object arg) {
		if(isInfoEnabled())
		{
			info(format.replace("{0}", arg.toString()));
		}		
	}

	@Override
	public void info(String format, Object arg1, Object arg2) {
		if(isInfoEnabled())
		{
			info(format.replace("{0}", arg1.toString()).replace("{1}", arg2.toString()));
		}
	}

	@Override
	public void info(String format, Object... arguments) {
		if(isInfoEnabled())
		{
			String output = format;
			int i = 0;
			for(Object arg : arguments) {
				output = output.replaceFirst("{"+ i +"}", arg.toString());
				i++;
			}
			info(output);		
		}
	}

	@Override
	public void info(String msg, Throwable t) {
		this.logger.logMessageWithException(INFO_LEVEL_NAME, msg, t);		
	}

	@Override
	public boolean isInfoEnabled(Marker marker) {
		//ignore marker..
		return this.isInfoEnabled();
	}

	@Override
	public void info(Marker marker, String msg) {
		//ignore marker..
		this.info(msg);
	}

	@Override
	public void info(Marker marker, String format, Object arg) {
		//ignore marker..
		this.info(format, arg);
	}

	@Override
	public void info(Marker marker, String format, Object arg1, Object arg2) {
		//ignore marker..
		this.info(format, arg1, arg2);
	}

	@Override
	public void info(Marker marker, String format, Object... arguments) {
		//ignore marker..
		this.info(format, arguments);
	}

	@Override
	public void info(Marker marker, String msg, Throwable t) {
		//ignore marker..
		this.info(msg, t);
	}

	@Override
	public boolean isWarnEnabled() {
		return this.logger.isLevelEnabled(WARNING_LEVEL_NAME);
	}

	@Override
	public void warn(String msg) {
		this.logger.logMessage(WARNING_LEVEL_NAME, msg);	
	}

	@Override
	public void warn(String format, Object arg) {
		if(isWarnEnabled())
		{
			warn(format.replace("{0}", arg.toString()));
		}		
	}

	@Override
	public void warn(String format, Object... arguments) {
		if(isWarnEnabled())
		{
			String output = format;
			int i = 0;
			for(Object arg : arguments) {
				output = output.replaceFirst("{"+ i +"}", arg.toString());
				i++;
			}
			warn(output);
		}
	}

	@Override
	public void warn(String format, Object arg1, Object arg2) {
		if(isWarnEnabled())
		{
			warn(format.replace("{0}", arg1.toString()).replace("{1}", arg2.toString()));
		}		
	}

	@Override
	public void warn(String msg, Throwable t) {
		this.logger.logMessageWithException(WARNING_LEVEL_NAME, msg, t);
	}

	@Override
	public boolean isWarnEnabled(Marker marker) {
		//ignore marker..
		return this.isWarnEnabled();
	}

	@Override
	public void warn(Marker marker, String msg) {
		//ignore marker..
		this.warn(msg);
	}

	@Override
	public void warn(Marker marker, String format, Object arg) {
		//ignore marker..
		this.warn(format, arg);		
	}

	@Override
	public void warn(Marker marker, String format, Object arg1, Object arg2) {
		//ignore marker..
		this.warn(format, arg1, arg2);
	}

	@Override
	public void warn(Marker marker, String format, Object... arguments) {
		//ignore marker..
		this.warn(format, arguments);	
	}

	@Override
	public void warn(Marker marker, String msg, Throwable t) {
		//ignore marker..
		this.warn(msg, t);
	}

	@Override
	public boolean isErrorEnabled() {
		return this.logger.isLevelEnabled(ERROR_LEVEL_NAME);
	}

	@Override
	public void error(String msg) {
		this.logger.logMessage(ERROR_LEVEL_NAME, msg);
	}

	@Override
	public void error(String format, Object arg) {
		if(isErrorEnabled())
		{
			error(format.replace("{0}", arg.toString()));
		}		
	}

	@Override
	public void error(String format, Object arg1, Object arg2) {
		if(isErrorEnabled())
		{
			error(format.replace("{0}", arg1.toString()).replace("{1}", arg2.toString()));
		}			
	}

	@Override
	public void error(String format, Object... arguments) {
		if(isErrorEnabled())
		{
			String output = format;
			int i = 0;
			for(Object arg : arguments) {
				output = output.replaceFirst("{"+ i +"}", arg.toString());
				i++;
			}
			error(output);
		}			
	}

	@Override
	public void error(String msg, Throwable t) {
		this.logger.logMessageWithException(ERROR_LEVEL_NAME, msg, t);
	}

	@Override
	public boolean isErrorEnabled(Marker marker) {
		//ignore marker..
		return this.isErrorEnabled();
	}

	@Override
	public void error(Marker marker, String msg) {
		//ignore marker..
		this.error(msg);		
	}

	@Override
	public void error(Marker marker, String format, Object arg) {
		//ignore marker..
		this.error(format, arg);
	}

	@Override
	public void error(Marker marker, String format, Object arg1, Object arg2) {
		//ignore marker..
		this.error(format, arg1, arg2);
	}

	@Override
	public void error(Marker marker, String format, Object... arguments) {
		//ignore marker..
		this.error(format, arguments);
	}

	@Override
	public void error(Marker marker, String msg, Throwable t) {
		//ignore marker..
		this.error(msg, t);
	}

}
